﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace BaseObj
{
    public class User
    {
        #region property 属性

        private string userId = "";
        private string userLoginId = "";
        private string password = "";
        private string groupId = "";
        private string state_Id = "";
        private string email = "";
        private string displayName = "";
        private string name = "";
        private string phone = "";
        private string sex_Id = "";
        private string[] roleIds = null;
        private bool isEffective = false;
        private string loginCount = "";
        private Group userGroup = null;

        /// <summary>
        /// 用户登录id
        /// </summary>
        public string UserLoginId
        {
            get { return userLoginId; }
            set { userLoginId = value; }
        }
        /// <summary>
        /// 用户所属组
        /// </summary>
        public Group UserGroup
        {
            get { return userGroup; }
        }

        public Group userGroup1;


        /// <summary>
        /// 上次登录时间
        /// </summary>
        public string LoginCount1
        {
            get { return loginCount; }
        }

        /// <summary>
        /// 登录次数
        /// </summary>
        public string LoginCount
        {
            get { return loginCount; }
        }
        private string lastLoginTime = "";

        /// <summary>
        /// 当使用userId建立此对象时，此属性表明对象是否可用，或是否成功获取了用户信息,或是否已经拥有角色
        /// </summary>
        public bool IsEffective
        {
            get { return isEffective; }
        }

        /// <summary>
        /// 该对象角色Id集合
        /// </summary>
        public string[] RoleIds
        {
            get { return roleIds; }
        }
        private Role[] roles = null;

        /// <summary>
        /// 该用户角色对象集合
        /// </summary>
        public Role[] Roles
        {
            get { return roles; }
        }

        /// <summary>
        /// 用户当前状态（表明是否有效或者被系统认可）
        /// </summary>
        public string State_Id
        {
            get { return state_Id; }
            set { state_Id = value; }
        }

        /// <summary>
        /// 用户自定义显示名称
        /// </summary>
        public string DisplayName
        {
            get { return displayName; }
            set { displayName = value; }
        }

        /// <summary>
        /// 用户真实姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 用户Emai
        /// </summary>
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        /// <summary>
        /// 用户联系电话
        /// </summary>
        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        /// <summary>
        /// 用户性别ID
        /// </summary>
        public string Sex_Id
        {
            get { return sex_Id; }
            set { sex_Id = value; }
        }
        
     
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return password; }
        }

        /// <summary>
        /// 用户所属组Id
        /// </summary>
        public string GroupId
        {
            get { return groupId; }
            set { groupId = value; }
        }
        
        /// <summary>
        /// 用户表中的主键ID
        /// </summary>
        public string UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        #endregion

        #region constructor 构造

        /// <summary>
        /// 根据用户登录id获取用户相关信息
        /// </summary>
        /// <param name="userLoginId_"></param>
        public User(string userLoginId_)
        {
            this.GetUserInfoByUserLoginId(userLoginId_);
        }

        #endregion

        #region private Mothed 私有方法

        /// <summary>
        /// 获取用户所有信息
        /// </summary>
        /// <param name="userId"></param>
        private void GetUserInfoByUserLoginId(string userLoginId_)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetUserInfoById");

            db.AddInParameter(dbCommand, "@userLoginId", System.Data.DbType.String, Check.CheckSql.CheckSqlStr(userLoginId_));
            DataTable tb = db.ExecuteDataSet(dbCommand).Tables[0];

            if (tb.Rows.Count == 1)
            {
                this.userId = tb.Rows[0]["id"].ToString();
                this.userLoginId = tb.Rows[0]["userLoginId"].ToString();
                this.password = tb.Rows[0]["password"].ToString();
                this.groupId = tb.Rows[0]["groupId"].ToString();
                this.state_Id = tb.Rows[0]["state_Id"].ToString();
                this.email = tb.Rows[0]["email"].ToString();
                this.displayName = tb.Rows[0]["displayName"].ToString();
                this.name = tb.Rows[0]["name"].ToString();
                this.phone = tb.Rows[0]["phone"].ToString();
                this.sex_Id = tb.Rows[0]["sex_Id"].ToString();
                this.loginCount = tb.Rows[0]["LoginCount"].ToString();
                this.lastLoginTime = tb.Rows[0]["LastLoginTime"].ToString();
                this.isEffective = true;
            }
            else 
            {
                this.isEffective = false;
                return;
            }

            //获取用户角色
            dbCommand = db.GetStoredProcCommand("GetRoleidsByUserId");
            db.AddInParameter(dbCommand, "@userLoginId", System.Data.DbType.String, Check.CheckSql.CheckSqlStr(userLoginId_));
            tb = db.ExecuteDataSet(dbCommand).Tables[0];

            if (tb.Rows.Count > 0)
            {
                this.roleIds = new string[tb.Rows.Count];
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    roleIds[i] = tb.Rows[i]["id"].ToString();
                }
            }
            else
            {
                this.isEffective = false;
                return;
            }

            this.roles = this.GetRoles(this.roleIds);

            //获取用户分组信息
            this.userGroup = new Group(this.groupId);
            return;
        }

        /// <summary>
        /// 获取某个角色Id的角色对象填充基本信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        private Role[] GetRoles(string[] roleIds)
        {
            if (roleIds != null)
            {
                Role[] roles = new Role[roleIds.Length];
                for (int i = 0; i < roleIds.Length; i++)
                {
                    Role rl = new Role(roleIds[i]);
                    roles[i] = rl;
                }
                return roles;
            }
            return null;
        }

       #endregion

        #region 公共方法
        /// <summary>
        /// 存储当前用户信息
        /// </summary>
        /// <returns></returns>
        public bool SaveThisUser()
        {
            if (this.userId != "")
            {
                //save
                return true;
            }
            else
            {
                //系统不允许保存空信息
                return false;
            }
        }

        /// <summary>
        /// 更新此用户的登录信息
        /// </summary>
        public void UpdateLogin(string userLoginId_)
        { 
            //更新登录信息
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UpdateLogin");
            db.AddInParameter(dbCommand, "@userLoginId", System.Data.DbType.String, Check.CheckSql.CheckSqlStr(userLoginId_));
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 返回用户此用户是否能够使用指定资源
        /// </summary>
        /// <param name="resourceId">资源id</param>
        /// <returns>是否能够使用指定资源</returns>
        public bool IsResourceCanUse(string resourceId)
        {
            if (this.isEffective)
            {
                if (this.roles.Length > 1)
                {
                    for (int i = 0; i < this.roles.Length;i++)
                    {
                        if (this.roles[i].Resources.Length > 1)
                        {
                            for (int j = 0; j < this.roles[i].Resources.Length; j++)
                            {
                                if (this.roles[i].Resources[j].ResourcId == resourceId)
                                {
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
                return false;
            }
            return false;
        }
        #endregion

    }
}